﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations.Core;
using Pixysoft.Framework.Configurations.Controller;

namespace Pixysoft.Framework.Configurations
{
    public class Configuration
    {
        /// <summary>
        /// 加载默认路径的配置文件
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static IConfigurationCollection Import()
        {
            IConfigurationCollection collection = ConfigurationController.Instance.GetConfiguration(
                    StringHelper.default_config_basepath + StringHelper.default_config_filename);

            return collection;
        }

        /// <summary>
        /// 加载指定路径的配置文件
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static IConfigurationCollection Import(string filename)
        {
            return Import(StringHelper.default_config_basepath, filename);
        }

        /// <summary>
        /// 加载指定路径的配置文件
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static IConfigurationCollection Import(string filepath, string filename)
        {
            IConfigurationCollection collection = ConfigurationController.Instance.GetConfiguration(
                    filepath + @"\" + filename);

            return collection;
        }


        /// <summary>
        /// 直接对字符串反序列化为配置集合
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static IConfigurationCollection DeserializeAll(string value)
        {
            return ConfigurationController.Instance.DeserializeConfigurations(value);
        }

        /// <summary>
        /// 查找符合要求 反序列化为配置
        /// </summary>
        /// <param name="value"></param>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static IConfiguration Deserialize(string value, string id, string name)
        {
            return ConfigurationController.Instance.DeserializeConfiguration(value, id, name);
        }

        /// <summary>
        /// 根据范型反序列化为对象 value是配置文件格式，不是对象格式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filepath"></param>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string value, string id, string name)
        {
            IConfiguration config = Deserialize(value, id, name);

            if (config == null)
                return default(T);

            string serialize = ConfigurationController.Instance.Serialize(config, true);

            //return Pixysoft.IO.XmlImporter.Instance.XmlDeserialize<T>(serialize);

            return XmlManager.Deserialize<T>(serialize);
        }


        /// <summary>
        /// 加载默认路径的配置文件 如果是集合就加载第一个 如果没有就报错
        /// </summary>
        public static IConfiguration Default
        {
            get
            {
                IConfigurationCollection collection = ConfigurationController.Instance.GetConfiguration(
                    StringHelper.default_config_basepath + StringHelper.default_config_filename);

                if (collection == null)
                    return null;

                if (collection.Count == 0)
                    return null;
                //throw new Exception("no config existed. please check disk.");

                return collection.GetConfiguration(0);
            }
        }


        /// <summary>
        /// 加载默认路径的配置文件中的子配置文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static IConfiguration GetConfiguration(string id)
        {
            string filepath = StringHelper.default_config_basepath + StringHelper.default_config_filename;

            return GetConfiguration(filepath, id, null);
        }

        /// <summary>
        /// 加载默认路径的配置文件中的子配置文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static IConfiguration GetConfiguration(string id, string name)
        {
            string filepath = StringHelper.default_config_basepath + StringHelper.default_config_filename;

            return GetConfiguration(filepath, id, name);
        }

        /// <summary>
        /// 加载指定路径的配置文件中的子配置文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static IConfiguration GetConfiguration(string filepath, string id, string name)
        {
            return ConfigurationController.Instance.GetConfiguration(filepath, id, name);
        }



        /// <summary>
        /// 反射默认路径的配置文件 id就是T的类型名字
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T GetConfiguration<T>()
        {
            return GetConfiguration<T>(typeof(T).Name);
        }

        /// <summary>
        /// 反射默认路径的配置文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T GetConfiguration<T>(string id)
        {
            IConfiguration config = GetConfiguration(id);

            if (config == null)
                return default(T);
            //throw new Exception("missing config for id " + id);

            string serialize = ConfigurationController.Instance.Serialize(config, true);

            //return Pixysoft.IO.XmlImporter.Instance.XmlDeserialize<T>(serialize);

            return XmlManager.Deserialize<T>(serialize);
        }

        /// <summary>
        /// 反射默认路径的配置文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T GetConfiguration<T>(string id, string name)
        {
            IConfiguration config = GetConfiguration(id, name);

            if (config == null)
                return default(T);
            //throw new Exception("missing config for id " + id);

            string serialize = ConfigurationController.Instance.Serialize(config, true);

            //return Pixysoft.IO.XmlImporter.Instance.XmlDeserialize<T>(serialize);

            return XmlManager.Deserialize<T>(serialize);
        }

        /// <summary>
        /// 反射指定路径的配置文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T GetConfiguration<T>(string filepath, string id, string name)
        {
            IConfiguration config = GetConfiguration(filepath, id, name);

            if (config == null)
                return default(T);
            //throw new Exception("missing config for id " + id);

            string serialize = ConfigurationController.Instance.Serialize(config, true);

            //return Pixysoft.IO.XmlImporter.Instance.XmlDeserialize<T>(serialize);

            return XmlManager.Deserialize<T>(serialize);
        }
    }
}
